package com.ruoyi.wlxz.service.impl;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.wlxz.domain.WxzStudentPublic;
import com.ruoyi.wlxz.domain.dto.StudentSupplement;
import com.ruoyi.wlxz.domain.vo.IfActivity;
import com.ruoyi.wlxz.mapper.WxzPublishedActivityMapper;
import com.ruoyi.wlxz.mapper.WxzStudentPublicMapper;
import com.ruoyi.wlxz.service.IWxzStudentPublicService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingQueue;
/**
 * 学生活动报名Service业务层处理
 *
 * @author 张浩
 * @date 2025-07-21
 */
@Service
public class WxzStudentPublicServiceImpl implements IWxzStudentPublicService {
    @Autowired
    private WxzStudentPublicMapper wxzStudentPublicMapper;
    @Autowired
    private WxzPublishedActivityMapper wxzPublishedActivityMapper;
    /**
     * 查询学生活动报名
     *
     * @param id 学生活动报名主键
     * @return 学生活动报名
     */
    @Override
    public WxzStudentPublic selectWxzStudentPublicById(Long id) {
        return wxzStudentPublicMapper.selectWxzStudentPublicById(id);
    }
    /**
     * 查询学生活动报名列表
     *
     * @param wxzStudentPublic 学生活动报名
     * @return 学生活动报名
     */
    @Override
    public List<WxzStudentPublic> selectWxzStudentPublicList(WxzStudentPublic wxzStudentPublic) {
        /*获取当前登录用户的nick_name和usename*/
        return wxzStudentPublicMapper.selectWxzStudentPublicList(wxzStudentPublic);
    }
    /**
     * 新增学生活动报名
     *
     * @param ifActivity 学生活动报名
     * @return 结果
     */
    private final BlockingQueue<IfActivity> queue = new LinkedBlockingQueue<>();
    public WxzStudentPublicServiceImpl() {
        // 启动一个线程处理队列中的请求
        new Thread(() -> {
            while (true) {
                try {
                    IfActivity ifActivity = queue.take();
                    processInsertWxzStudentPublic(ifActivity);
                } catch (InterruptedException e) {
                    Thread.currentThread().interrupt();
                }
            }
        }).start();
    }
    @Override
    public int insertWxzStudentPublic(IfActivity ifActivity) {
        try {
            queue.put(ifActivity);
            // 这里可以返回一个临时状态，实际处理结果可以通过其他方式通知用户
            return 1;
        } catch (InterruptedException e) {
            Thread.currentThread().interrupt();
            return -1;
        }
    }
    private int processInsertWxzStudentPublic(IfActivity ifActivity) {
        WxzStudentPublic wxzStudentPublic = new WxzStudentPublic();
        Long activityId = ifActivity.getActivityId();
        Long participantLimit = ifActivity.getParticipantLimit();
        String name = ifActivity.getName();
        String snumber = ifActivity.getSnumber();
        wxzStudentPublic.setStudentName(name);
        wxzStudentPublic.setStudentNumber(snumber);
        wxzStudentPublic.setActivityId(activityId);
        // 查询已报名人数
        int totalNum = wxzStudentPublicMapper.selectTotalNumByActivityId(activityId);
        if (totalNum >= participantLimit) {
            //在人数已满的情况下,报名就返回空,想拼手速??没门
            WxzStudentPublic wxzStudentPublic1 = wxzStudentPublicMapper.selectWxzStudentPublicByActivityIdAndStudentNumber(wxzStudentPublic);
            if (wxzStudentPublic1 != null) {
                return -1;
            }
            //如果第一次报名,但是人满了,很可惜,未录取
            wxzStudentPublic.setStatus("未录取");
            return wxzStudentPublicMapper.insertWxzStudentPublic(wxzStudentPublic);
        } else {
            //看看是否是有人退出之后,才报名的,如果是这样,就修改字段,而不是新增字段了,虽然顺序会变,但是无伤大雅
            WxzStudentPublic current = wxzStudentPublicMapper.selectWxzStudentPublicByActivityIdAndStudentNumber(wxzStudentPublic);
            if (current != null) {
                wxzStudentPublic.setStatus("已录取");
                return wxzStudentPublicMapper.updateWxzStudentPublic(wxzStudentPublic);
            }
            wxzStudentPublic.setStatus("已录取");
        }
        return wxzStudentPublicMapper.insertWxzStudentPublic(wxzStudentPublic);
    }
    /**
     * 修改学生活动报名
     *
     * @param wxzStudentPublic 学生活动报名
     * @return 结果
     */
    @Override
    public int updateWxzStudentPublic(WxzStudentPublic wxzStudentPublic) {
        wxzStudentPublic.setUpdateTime(DateUtils.getNowDate());
        return wxzStudentPublicMapper.updateWxzStudentPublic(wxzStudentPublic);
    }
    /**
     * 批量删除学生活动报名
     *
     * @param ids 需要删除的学生活动报名主键
     * @return 结果
     */
    @Override
    public int deleteWxzStudentPublicByIds(Long[] ids) {
        return wxzStudentPublicMapper.deleteWxzStudentPublicByIds(ids);
    }
    /**
     * 删除学生活动报名信息
     *
     * @param id 学生活动报名主键
     * @return 结果
     */
    @Override
    public int deleteWxzStudentPublicById(Long id) {
        return wxzStudentPublicMapper.deleteWxzStudentPublicById(id);
    }
    /**
     * 根据id查询报名的学生和学分补录信息
     *
     * @param acitivityId 活动id
     * @return 学生补录信息
     */
    @Override
    public List<StudentSupplement> selectStudentSupplementList(int acitivityId) {
        List<StudentSupplement> studentSupplements = wxzStudentPublicMapper.selectStudentSupplementList(acitivityId);
        return studentSupplements;
    }
    /**
     * 根据活动id和学生学号查询学生活动报名信息
     */
    @Override
    public WxzStudentPublic selectWxzStudentPublicByActivityIdAndStudentNumber(WxzStudentPublic wxzStudentPublic) {
        return wxzStudentPublicMapper.selectWxzStudentPublicByActivityIdAndStudentNumber(wxzStudentPublic);
    }
}